#include "QGanttChartHourColumnHeader.hpp"
#include "QtProj/QAbstractGanttModel.qth"

namespace QtProj
{

	void QGanttChartHourColumnHeader::paint( QHeaderPaintContext& ctx)
	{
		// Drawing the line at the bottom of the header.
		ctx.painter().setPen( QColor(128,128,128));
		ctx.painter().drawLine( 
			ctx.headerRect().left(), ctx.headerRect().bottom()-1, ctx.headerRect().right(), ctx.headerRect().bottom()-1);

		// Draw the hour columns and the day columns when the day is changing.
		
		// Approximate the number of hour columns that need to be drawn.
		int numberOfColumns = this->calculateNumberOfColumns( ctx);
		// Calculate the middle line between the cells.
		int middleVert = this->calculateMiddleLineLevel( ctx);

		// It contains the start coordinate (horizontal) of the current cell to be drawn.
		int cellStart = ctx.headerRect().left();
		// It contains the start coordinate (horizontal) of the day cell to be drawn.
		int dayCellStart = cellStart;
		// It contains the current time of the cell to be drawn.
		QDateTime curTime = ctx.firstColumnTime();
		// It contains the date of the current day to be drawn.
		QDate oldDate = curTime.date();
		while( numberOfColumns > 0)
		{
			this->paintHourCell( ctx, curTime, cellStart, middleVert);
			--numberOfColumns;

			// If day change detected, then draw the day column.
			QDate curDate = curTime.date();
			if( curDate != oldDate)
			{
				this->paintDayCell( ctx, oldDate, cellStart, dayCellStart, middleVert);
				oldDate = curDate;
				dayCellStart = cellStart;
			}

			// Go to the next hour
			curTime = curTime.addSecs( 3600);
			cellStart+=ctx.columnWidth();
		}

		this->paintDayCell( ctx, oldDate, cellStart, dayCellStart, middleVert);
		ctx.painter().drawLine( 
			ctx.headerRect().left(), middleVert, ctx.headerRect().right(), middleVert);
	}

// ---------------------------------------------------------------------------------------------------------------------

	void QGanttChartHourColumnHeader::paintHourCell( QHeaderPaintContext& ctx, const QDateTime& cellTime, int cellStart, int middleVert)
	{
		// Draw left line of the current hour cell
		ctx.painter().drawLine( cellStart, middleVert, cellStart, ctx.headerRect().bottom() - 1);
		// Draw the hour text.
		QString hourString = cellTime.toString( "HH");
		ctx.painter().drawText( 
			cellStart + 2, middleVert + 2, ctx.columnWidth() - 4, middleVert - 4,
			Qt::AlignCenter,
			hourString);
	}

// ---------------------------------------------------------------------------------------------------------------------

	void QGanttChartHourColumnHeader::paintDayCell( QHeaderPaintContext& ctx, const QDate& cellDate, int dayCellStart, int dayCellEnd, int bottom)
	{
		ctx.painter().drawLine( dayCellEnd, ctx.headerRect().top(), dayCellEnd, bottom);
		QString dateStr = cellDate.toString( "dd MMM yy");
		ctx.painter().drawText(
			dayCellStart + 2, ctx.headerRect().top() + 1, dayCellEnd - dayCellStart - 4, (ctx.headerRect().height()/2) - 4,
			Qt::AlignLeft | Qt::AlignVCenter,
			dateStr);		
	}

// ---------------------------------------------------------------------------------------------------------------------

	int QGanttChartHourColumnHeader::maxNumberOfColumns( const QAbstractGanttModel& model) const
	{
		int seconds = model.minDate().secsTo( model.maxDate());
		return seconds / 3600;
	}

} // namespace QtProj
